// pages/message/chart/index.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
const util = require('../../utils/util')
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: '',
        // 当前登录者信息
        login: {},
        page: {
            pageNo: 1,
            pageSize: 100
        },
        // 聊天信息
        letterList: [],
        userid: 0,
        inter: '',
        kes: false,
        departments: [],
        department: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // let id = options.userid
        // let _this = this
        this.setData({
            login: app.globalData.userInfo,
        })
        this.getDepartments();
    },
    // 输入监听
    inputClick(e) {
        this.setData({
            content: e.detail.value
        })
    },
    getDepartments() {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "GET",
            showLoading: true,
            url: `/api/department/list`,
            message: "正在获取...",
            // data:_this.data.card,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                var depts = []
                for (var i = 0; i < res.data.data.length; i++) {
                    for (var j = 0; j < res.data.data[i].children.length; j++) {
                        depts.push(res.data.data[i].children[j])
                        if (res.data.data[i].children[j].dept_name == '胃肠外科') {
                            this.setData({
                                department: res.data.data[i].children[j]
                            })
                        }
                    }
                }
                _this.setData({
                    departments: depts
                })
                for (var i = 0; i < _this.data.departments.length; i++) {
                    _this.data.departments[i].open = false
                }
            }
        }).catch(err => {
            console.log(err)
        });
    },
    // 发送监听
    sendClick() {
        var that = this;
        var list = this.data.letterList;
        // 获取当前时间
        var date = new Date();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minu = date.getMinutes();
        var now1 = month < 10 ? '0' + month : month;
        var now2 = day < 10 ? '0' + day : day;
        // 组装数据
        var msg = {
            conversation: {
                content: this.data.content,
                from_id: this.data.login.id,
                time: now1 + '-' + now2 + ' ' + hour + ':' + minu,
            },
            target: this.data.login
        }

        var msg2 = {
            conversation: {
                content: '根据描述，建议前往以下科室就诊。如有健康相关问题，可以随时问我哦',
                from_id: -1,
                time: now1 + '-' + now2 + ' ' + hour + ':' + minu,
            },
            target: {
                avatar_url: "../../images/机器人.png"
            }
        }
        if (this.data.content != '我肚子疼') {
            var randomNumber = Math.floor(Math.random() * this.data.departments.length) + 1;
            this.setData({
                department: this.data.departments[randomNumber]
            })
        }
        console.log(this.data.department)
        list = list.concat(msg)
        list = list.concat(msg2)
        this.setData({
            letterList: list,
        }, () => {
            that.scrollToBottom();
            that.setData({
                content: '',
                kes: true,
            })
        })

    },
    // 滑动到最底部
    scrollToBottom() {
        setTimeout(() => {
            wx.pageScrollTo({
                scrollTop: 200000,
                duration: 3
            });
        }, 600)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        let _this = this
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})